package com.jeesite.modules.kj.api.controller.member;


import com.alibaba.fastjson.JSON;
import com.jeesite.common.cache.CacheUtils;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.msg.SmsUtils;
import com.jeesite.modules.aliyun.msg.AliyunSmsUtils;
import com.jeesite.modules.kj.api.vo.request.UpdateInfoRequestVo;
import com.jeesite.modules.kj.api.vo.response.DictResponseVo;
import com.jeesite.modules.kj.api.vo.response.MemberInfoResponseVo;
import com.jeesite.modules.common.interfaces.CurrentUser;
import com.jeesite.modules.common.interfaces.UserLoginToken;
import com.jeesite.modules.kj.api.enumCode.UnicomResponseEnums;
import com.jeesite.modules.kj.api.vo.response.ServiceTypeResponseVo;
import com.jeesite.modules.kj.api.vo.responseVo.ResponseBean;
import com.jeesite.modules.kj.entity.KjMember;
import com.jeesite.modules.kj.entity.KjMemberIntegralHistory;
import com.jeesite.modules.kj.entity.KjServiceType;
import com.jeesite.modules.kj.entity.StarLevelEntity;
import com.jeesite.modules.kj.service.KjMemberIntegralHistoryService;
import com.jeesite.modules.kj.service.KjMemberIntegralsService;
import com.jeesite.modules.kj.service.KjMemberService;
import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.utils.DictUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:hzy
 * Date:2021/07/04
 * Description:用户信息
 */
@Api(value = "用户信息api", tags = {"用户信息api"})
@Slf4j
@Controller
@RequestMapping(value = "/api/member")
public class KjMemberApiController {
    private final static Logger logger = LoggerFactory.getLogger(KjMemberApiController.class);
    @Autowired
    private KjMemberService kjMemberService;
    @Autowired
    private KjMemberIntegralsService kjMemberIntegralsService;
    @Autowired
    private KjMemberIntegralHistoryService kjMemberIntegralHistoryService;

    @ApiOperation(value = "获取证件类型列表", notes = "获取服务类型列表")
    @GetMapping(value = "documentTypeList")
    public ResponseBean<List<DictResponseVo>> documentTypeList() {
        List<DictData> dictList = DictUtils.getDictList("document_type");
        List<DictResponseVo> vos = ListUtils.newArrayList();
        for (DictData dict: dictList
        ) {
            vos.add(new DictResponseVo(dict));
        }
        return new ResponseBean<>(vos);
    }


    @ApiOperation(value = "用户信息", notes = "用户详细信息")
    @GetMapping(value = "getInfo")
    @UserLoginToken
    public ResponseBean<MemberInfoResponseVo> getInfo(@ApiIgnore @CurrentUser String memberId) {
        KjMember kjMember = kjMemberService.get(memberId);
        if(kjMember != null){
            MemberInfoResponseVo vo = new MemberInfoResponseVo(kjMember);
            return new ResponseBean<>(vo);
        }
        return new ResponseBean<>(Global.FALSE, UnicomResponseEnums.NO_USER_EXIST);
    }


    @ApiOperation(value = "更新用户信息", notes = "更新用户信息")
    @PostMapping(value = "updateInfo")
    @UserLoginToken
    @ResponseBody
    public ResponseBean<MemberInfoResponseVo> updateInfo(@ApiIgnore @CurrentUser String memberId, UpdateInfoRequestVo vo) {
        KjMember kjMember = vo.convertToMember();
        kjMember.setId(memberId);
        kjMemberService.update(kjMember);
        return new ResponseBean(Global.TRUE, "更新成功");
    }



    @ApiOperation(value = "发送更改手机号验证码", notes = "发送更改手机号验证码")
    @PostMapping(value = "changeMobileSms")
    @UserLoginToken
    @ResponseBody
    public ResponseBean<Object> changeMobileSms(@ApiIgnore @CurrentUser String memberId, String mobile) {
        KjMember where = new KjMember();
        where.setMobile(mobile);
        KjMember kjMember = kjMemberService.getFirstMember(where);
        if(kjMember == null){
            //生成短信码
            kjMemberService.sendChangeMobileSms(mobile);
            return new ResponseBean(Global.TRUE, "发送成功");
        }else{
            return new ResponseBean(Global.FALSE, "", "该手机号已注册用户");
        }
    }



    @ApiOperation(value = "更改手机号", notes = "更改手机号")
    @PostMapping(value = "changeMobile")
    @UserLoginToken
    @ResponseBody
    public ResponseBean<Object> changeMobile(@ApiIgnore @CurrentUser String memberId, String mobile, String code) {
        KjMember kjMember = kjMemberService.get(memberId);
        if(kjMemberService.getChangeMobileCode(mobile).equals(code)) {
            //生成短信码
            kjMember.setMobile(mobile);
            kjMemberService.update(kjMember);
            return new ResponseBean(Global.TRUE, "手机号修改成功");
        }else{
            return new ResponseBean(Global.FALSE, "", "验证码错误");
        }

    }


    @ApiOperation(value = "获取当前星级", notes = "获取当前星级")
    @GetMapping(value = "getStarLevel")
    @UserLoginToken
    public ResponseBean<StarLevelEntity> getStarLevel(@ApiIgnore @CurrentUser String memberId) {
        KjMember kjMember = kjMemberService.get(memberId);
        //
        BigDecimal[] levels = new BigDecimal[]{BigDecimal.valueOf(21),
                BigDecimal.valueOf(51), BigDecimal.valueOf(101), BigDecimal.valueOf(201), BigDecimal.valueOf(301)};
        BigDecimal totalIntegrals = kjMemberIntegralsService.findByMemberId(memberId).getTotalIntegrals();
        BigDecimal targetLevelIntegral = BigDecimal.ZERO;
        int level = 0;
        for (int i = 0; i < levels.length; i++) {
            if(totalIntegrals.compareTo(levels[i]) >= 0){
                level = i + 1;
                targetLevelIntegral = levels[i];
            }
        }
        KjMemberIntegralHistory where = new KjMemberIntegralHistory();
        where.setIntegral(targetLevelIntegral);
        where.setKjMember(kjMember);
        StarLevelEntity entity = kjMemberIntegralHistoryService.getStarLevel(where);
        if(entity == null){
            entity = new StarLevelEntity();
            entity.setGainTime(null);
        }
        entity.setLevel("" + level);
        return new ResponseBean(Global.TRUE, entity);

    }



    @ApiOperation(value = "获取分享证书编号", notes = "获取分享证书编号")
    @GetMapping(value = "getShareNo")
    @UserLoginToken
    public ResponseBean<String> getShareNo(@ApiIgnore @CurrentUser String memberId) {
        KjMember kjMember = kjMemberService.get(memberId);
        return new ResponseBean(Global.TRUE, kjMemberService.getShareNo(kjMember));

    }

}
